Table of Contents
Using PdfPathContent

PdfPathContent is used to draw lines and shapes on a page. To use PdfPathContent, you must define the path first, then either stroke it (to create lines) or fill it (to create shapes). Follow these steps to use PdfPathContent object:

  1. create a PdfPathContent object;

    //Create a new PdfPathContent
    EO.Pdf.Contents.PdfPathContent content = new EO.Pdf.Contents.PdfPathContent();
  2. create a PdfSubPath object and set the path's starting point:

    //Create a new sub path and set the sub path's starting point
    EO.Pdf.Drawing.PdfSubPath subPath = new EO.Pdf.Drawing.PdfSubPath();
    subPath.From = new EO.Pdf.Drawing.PdfPoint(100, 100);

    A "sub path" is a sequence of connected segments. One path can contain multiple disconnected sub paths.

  3. Add one or more a PdfPathSegment object into the sub path:

    //Draw a line from the current position to (200, 200)
    EO.Pdf.Drawing.PdfPathLineSegment line = 
        new EO.Pdf.Drawing.PdfPathLineSegment(new EO.Pdf.Drawing.PdfPoint(200, 200));
    subPath.Segments.Add(line);

    You can also add other segments such as curves or rectangles.

  4. Add the sub path into the path's SubPaths collection:

    //Add the sub path into the path
    content.Path.SubPaths.Add(subPath);

    You can create and add as many sub paths as needed;

  5. set the PdfPathContent's Action property to stroke or fill the path. To stroke the path, you also need to set the current stroking color and line width:

    //Stroke the path
    content.LineWidth = 1f;
    content.StrokingColor = new EO.Pdf.Drawing.PdfColor(Color.Red);
    content.Action = EO.Pdf.Contents.PdfPathPaintAction.Stroke;

    To fill the path, you need to set the current non-stroking color and set the Action property to Fill:

    //Set the non stroking color
    content.NonStrokingColor = new EO.Pdf.Contents.PdfColor(System.Drawing.Color.Red);              
                  
    //Fill the path
    content.Action = PdfPathPaintAction.Fill;
  6. Add the PdfPathContent to the page's Contents collection:
    //Add the path into the page
    page.Contents.Add(content);

Below is the full code. The code creates a single red line from (100, 100) to (200, 200).

//Create a new document
PdfDocument doc = new PdfDocument();

//Add a new page
PdfPage page = doc.Pages.Add();

//Create a new PdfPathContent
EO.Pdf.Contents.PdfPathContent content = new EO.Pdf.Contents.PdfPathContent();

//Create a new sub path and set the sub path's starting point
EO.Pdf.Drawing.PdfSubPath subPath = new EO.Pdf.Drawing.PdfSubPath();
subPath.From = new EO.Pdf.Drawing.PdfPoint(100, 100);

//Draw a line from the current position to (200, 200)
EO.Pdf.Drawing.PdfPathLineSegment line = 
    new EO.Pdf.Drawing.PdfPathLineSegment(new EO.Pdf.Drawing.PdfPoint(200, 200));
subPath.Segments.Add(line);

//Add the sub path into the path
content.Path.SubPaths.Add(subPath);

//Stroke the path
content.LineWidth = 1f;
content.StrokingColor = new EO.Pdf.Drawing.PdfColor(Color.Red);
content.Action = EO.Pdf.Contents.PdfPathPaintAction.Stroke;

//Add the path into the page
page.Contents.Add(content);

//Save the document
doc.Save("hello.pdf");